package com.bt.lms.network.msg.packet;

import com.bt.lms.network.msg.ReqMsg;
import com.bt.lms.network.msg.RespMsg;
import com.bt.lms.utils.Convert;

/**
 * 响应消息包
 *
 * @author 徐大永
 */
public class RespMsgPacket {

    /**
     * 魔数
     */
    private byte[] magicNo;

    /**
     * 协议版本号
     */
    private byte version = ReqMsg.NETWORK_VERSION_ONE;

    /**
     * 序列化算法
     */
    private byte serialCou = ReqMsg.NETWORK_SERIAL_COU_JSON;

    /**
     * 事件类型
     */
    private byte evenType = ReqMsg.NETWORK_EVEN_TYPE_BUSINESS;

    /**
     * 业务类型
     */
    private byte[] bizType;

    /**
     * 请求id 毫秒数
     */
    private byte[] reqId;

    /**
     * 保留字段
     */
    private byte[] remainField = new byte[ReqMsg.NETWORK_REMAIN_FIELD_SIZE];

    /**
     * 状态
     */
    private byte[] status;

    /**
     * 数据长度
     */
    private byte[] dataLen;

    /**
     * 数据
     */
    private byte[] data;



    /**
     * 转响应消息
     *
     * @return 响应消息
     */
    public RespMsg toRespMsg() {

        //要返回的对象
        RespMsg respMsg = new RespMsg();

        //1.消息头
        //魔数
        byte[] magicNoBytes = this.getMagicNo();
        String magicNo = new String(magicNoBytes);
        respMsg.setMagicNo(magicNo);
        //协议版本号
        byte version = this.getVersion();
        respMsg.setVersion(version);
        //序列化算法
        byte serialCou = this.getSerialCou();
        respMsg.setSerialCou(serialCou);
        //事件类型
        byte evenType = this.getEvenType();
        respMsg.setEvenType(evenType);
        //业务类型
        byte[] bizTypeBytes = this.getBizType();
        short bizType = Convert.bytesToShort(bizTypeBytes);
        respMsg.setBizType(bizType);
        //请求id
        byte[] reqIDBytes = this.getReqId();
        Long reqId = Convert.bytesToLong(reqIDBytes);
        respMsg.setReqId(reqId);
        //保留字段
        byte[] remainFieldBytes = this.getRemainField();
        respMsg.setRemainField(remainFieldBytes);
        //状态
        byte[] statusBytes = this.getStatus();
        short status = Convert.bytesToShort(statusBytes);
        respMsg.setStatus(status);

        //消息体
        byte[] dataBytes = this.getData();
        String data = new String(dataBytes);
        respMsg.setData(data);

        return respMsg;
    }

    /**
     * 转响应消息
     *
     * @param sumBytes 总字节
     * @return 响应消息
     */
    public static RespMsg thRespMsg(byte[] sumBytes) {

        //要返回的对象
        RespMsg respMsg = new RespMsg();

        //数组拷贝角标
        int index = 0;

        //1.消息头
        //魔数
        byte[] magicNoBytes = new byte[ReqMsg.NETWORK_MAGIC_NO_LEN];
        System.arraycopy(sumBytes,index,magicNoBytes,0,ReqMsg.NETWORK_MAGIC_NO_LEN);
        index += ReqMsg.NETWORK_MAGIC_NO_LEN;
        String magicNo = new String(magicNoBytes);
        respMsg.setMagicNo(magicNo);
        //协议版本号
        byte version = sumBytes[index++];
        respMsg.setVersion(version);
        //序列化算法
        byte serialCou = sumBytes[index++];
        respMsg.setSerialCou(serialCou);
        //事件类型
        byte evenType = sumBytes[index++];
        respMsg.setEvenType(evenType);
        //业务类型
        byte[] bizTypeBytes = new byte[ReqMsg.NETWORK_BIZ_TYPE_LEN];
        System.arraycopy(sumBytes,index,bizTypeBytes,0,ReqMsg.NETWORK_BIZ_TYPE_LEN);
        index += ReqMsg.NETWORK_BIZ_TYPE_LEN;
        short bizType = Convert.bytesToShort(bizTypeBytes);
        respMsg.setBizType(bizType);
        //请求id
        byte[] reqIDBytes = new byte[ReqMsg.NETWORK_REQ_ID_LEN];
        System.arraycopy(sumBytes,index,reqIDBytes,0,ReqMsg.NETWORK_REQ_ID_LEN);
        index += ReqMsg.NETWORK_REQ_ID_LEN;
        Long reqId = Convert.bytesToLong(reqIDBytes);
        respMsg.setReqId(reqId);
        //保留字段
        byte[] remainFieldBytes = new byte[ReqMsg.NETWORK_REMAIN_FIELD_SIZE];
        System.arraycopy(sumBytes,index,remainFieldBytes,0,ReqMsg.NETWORK_REMAIN_FIELD_SIZE);
        index += ReqMsg.NETWORK_REMAIN_FIELD_SIZE;
        respMsg.setRemainField(remainFieldBytes);
        //状态
        byte[] statusBytes = new byte[RespMsg.NETWORK_STATUS_LEN];
        System.arraycopy(sumBytes,index,statusBytes,0,RespMsg.NETWORK_STATUS_LEN);
        index += RespMsg.NETWORK_STATUS_LEN;
        short status = Convert.bytesToShort(statusBytes);
        respMsg.setStatus(status);
        //数据长度
        byte[] dataLenBytes = new byte[ReqMsg.NETWORK_DATA_LEN];
        System.arraycopy(sumBytes,index,dataLenBytes,0,ReqMsg.NETWORK_DATA_LEN);
        index += ReqMsg.NETWORK_DATA_LEN;
        int dataLen = Convert.bytesToInt(dataLenBytes);

        //消息体
        byte[] dataBytes = new byte[dataLen];
        System.arraycopy(sumBytes,index,dataBytes,0,dataLen);
        String data = new String(dataBytes);
        respMsg.setData(data);

        return respMsg;
    }





    public byte[] getMagicNo() {
        return magicNo;
    }

    public void setMagicNo(byte[] magicNo) {
        this.magicNo = magicNo;
    }

    public byte getVersion() {
        return version;
    }

    public void setVersion(byte version) {
        this.version = version;
    }

    public byte getSerialCou() {
        return serialCou;
    }

    public void setSerialCou(byte serialCou) {
        this.serialCou = serialCou;
    }

    public byte getEvenType() {
        return evenType;
    }

    public void setEvenType(byte evenType) {
        this.evenType = evenType;
    }

    public byte[] getBizType() {
        return bizType;
    }

    public void setBizType(byte[] bizType) {
        this.bizType = bizType;
    }

    public byte[] getReqId() {
        return reqId;
    }

    public void setReqId(byte[] reqId) {
        this.reqId = reqId;
    }

    public byte[] getRemainField() {
        return remainField;
    }

    public void setRemainField(byte[] remainField) {
        this.remainField = remainField;
    }

    public byte[] getStatus() {
        return status;
    }

    public void setStatus(byte[] status) {
        this.status = status;
    }

    public byte[] getDataLen() {
        return dataLen;
    }

    public void setDataLen(byte[] dataLen) {
        this.dataLen = dataLen;
    }

    public byte[] getData() {
        return data;
    }

    public void setData(byte[] data) {
        this.data = data;
    }
}
